package avicit.bdp.dgs.qa.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dgs.qa.dao.WorkOrderHistoryDAO;
import avicit.bdp.dgs.qa.dto.WorkOrderHistoryDTO;
import avicit.bdp.dgs.qa.utils.enums.WorkOrderState;
import avicit.platform6.api.system.ConvertColumnClient;
import avicit.platform6.core.exception.BusinessException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-05-29
 * @类说明：WorkOrderHistoryService
 * @修改记录：
 * @注意事项：
 * @主要功能：工单历史记录Service
 */
@Service
public class WorkOrderHistoryService extends BaseService<WorkOrderHistoryDAO, WorkOrderHistoryDTO> {
    private static final Logger logger = LoggerFactory.getLogger(JobService.class);

    @Autowired
    private ConvertColumnClient convertColumnClient;

    /**
     * 新增工单历史记录
     * @param historyDTO
     * @return
     */
    @Transactional
    public String addHistory(WorkOrderHistoryDTO historyDTO) {
        checkAddValid(historyDTO);

        save2Db(historyDTO);

        return historyDTO.getId();
    }

    /**
     * 根据工单ID查询历史记录
     * @param workOrderId
     * @return
     */
    @Transactional
    public List<WorkOrderHistoryDTO> getWorkOrderHistoryList(String workOrderId) {
        if (StringUtils.isBlank(workOrderId)) {
            logger.warn("工单ID为空，未找到历史记录");
            return new ArrayList<>();
        }

        List<WorkOrderHistoryDTO> historyList = this.mapper.getWorkOrderHistoryList(workOrderId);
        if (CollectionUtils.isEmpty(historyList)) {
            logger.warn("未找到历史记录,工单ID=" + workOrderId);
            return new ArrayList<>();
        }

        valueHistoryConvert(historyList);

        return historyList;
    }

    /**
     * 删除工单历史记录
     * @param workOrderId
     * @return
     */
    @Transactional
    public int deleteWorkOrderHistory(String workOrderId) {
        if (StringUtils.isBlank(workOrderId)) {
            logger.warn("工单ID为空，删除历史记录失败");
            return 0;
        }

        return this.mapper.deleteWorkOrderHistoryByCond(workOrderId);
    }

    /**
     * 新建工单历史记录时合法性检查
     * @param historyDTO
     */
    private void checkAddValid(WorkOrderHistoryDTO historyDTO) {
        if (historyDTO == null) {
            throw new BusinessException("入参为空.");
        }
        if (StringUtils.isBlank(historyDTO.getId())) {
            throw new BusinessException("工单主键ID为空.");
        }
        if (StringUtils.isBlank(historyDTO.getWorkOrderId())) {
            throw new BusinessException("工单ID为空.");
        }
        if (historyDTO.getState() == null) {
            throw new BusinessException("工单状态为空.");
        }
        if (StringUtils.isBlank(historyDTO.getContent())) {
            throw new BusinessException("工单处理意见为空.");
        }
    }

    /**
     * 持久化DB
     * @param historyDTO
     */
    private void save2Db(WorkOrderHistoryDTO historyDTO) {
        if (historyDTO != null) {
            // 持久化
            this.insert(historyDTO);

            // 审计日志
            BdpLogUtil.log4Insert(historyDTO);
        }
    }

    /**
     * 通过平台API将字段值转换为名称，包括通用选择组件、通用代码
     * @param dtoList
     */
    private void valueHistoryConvert(List<WorkOrderHistoryDTO> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return;
        }

        // 设置枚举类型
        for (WorkOrderHistoryDTO dto : dtoList) {
            dto.setStateName(WorkOrderState.getDescByCode(dto.getState()));
        }
    }
}
